<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use think\Controller;
use think\Request;

class Index extends Controller
{
    /**
     * index 首页
     * tuichu 退出页
     * personal 个人页
     */
    public function index()
    {
        $data =(new Admin())->insert();
        return view('admin@index/index',compact('data'));
    }
    public function post()
    {
        $admin =session('admin_user')['username'];
        return view('admin@index/post',compact('admin'));
    }
    public function postpassword(Request $request)
    {
        $input =$request->post();
        return $ret =(new Admin())->putLogin($input);
    }
    public function tuichu()
    {
        session(null);
        return redirect(url('admin'));
    }
    public function personal(Request $request)
    {
        if($request->isGet()) {
            $data = (new Admin())->insert();
            return view('admin@index/personal', compact('data'));
        }else if($request->isPost()){
            $data =$request->post();
            return (new Admin())->personal($data);
        }
    }
    public function update(Request $request){
        //接收图片信息
        $file =$request->file('mylogo');
        //附件存储临时位置
        $path ="./uploads/mylogo";
        //系统会给附件创建一个随机的字符串
        $result = $file->move($path);
        if($result){
            //上传成功，返回当前上传的附件路径名，用于客户端使用
            $logoname =$result->getPathname();
            $logo =str_replace("\\","/",$logoname);
            $info =['status'=>'success','logo'=>$logo];
            echo json_encode($info);
        }else{
            $info =['status'=>'failure'];
            echo json_encode($info);
        }
    }
}
